package com.fzl.spring_chatroom.controller;

import com.fzl.spring_chatroom.model.dao.FriendRequest;
import com.fzl.spring_chatroom.service.FriendRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/friend/request")
public class FriendRequestController {

    @Autowired
    private FriendRequestService friendRequestService;

    @PostMapping("/send")
    public String sendRequest(@RequestParam Integer fromUserId, @RequestParam Integer toUserId) {
        boolean result = friendRequestService.sendFriendRequest(fromUserId, toUserId);
        return result ? "请求已发送" : "已是好友或已发送请求";
    }

    @GetMapping("/pending")
    public List<FriendRequest> getPending(@RequestParam Integer userId) {
        return friendRequestService.getPendingRequests(userId);
    }

    @PostMapping("/handle")
    public Map<String, Object> handleRequest(@RequestParam Integer requestId, @RequestParam String action) {
        boolean result = friendRequestService.handleRequest(requestId, action);
        Map<String, Object> resp = new HashMap<>();
        resp.put("code", result ? 0 : 1);
        resp.put("msg", result ? "操作成功" : "操作失败");
        return resp;
    }
}